package de.busschedule.view;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.busschedule.R;
import de.busschedule.logging.Logger;

public class MainActivity extends Activity{
	private final String TAG = "MainActivity";
	private ListView list;
	private List<String> menuItems;

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// read the properties
		readProperties();
	
		final Resources res = getResources();
		menuItems = new ArrayList<String>();
		menuItems.add(res.getString(R.string.list_lines));
		menuItems.add(res.getString(R.string.list_stops));
		
		//Build the list
		list = (ListView) findViewById(R.id.list); 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, menuItems);
		list.setAdapter(adapter);

		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
				Intent intent = null;
				if(list.getAdapter().getItem(arg2).toString().equals(res.getString(R.string.list_lines))){
					intent = new Intent(MainActivity.this, LineListActivity.class);
				}else if(list.getAdapter().getItem(arg2).toString().equals(res.getString(R.string.list_stops))){
					intent = new Intent(MainActivity.this, StopListActivity.class);
				}else{
					Logger.wtf(TAG, "Unknown Item clicked");
				}
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Liest Properties aus einer Konfigurationsdatei ein und setzt diese.
	 */
	private void readProperties(){
		// Datei einlesen
		Resources res = this.getResources();
		Properties prop = new Properties();
		try{
			InputStream rawResource = res.openRawResource(R.raw.config);
			prop.load(rawResource);
		}catch(NotFoundException e){
			Log.e(TAG, e.getMessage());
		}catch(IOException e){
			Log.e(TAG, e.getMessage());
		}

		// Logging ein oder aus
		Logger.setLoggable(prop.getProperty("loggingEnabled"));
		// LogLevel setzen
		String level = prop.getProperty("logLevel").toString();
		Logger.setLogLevel(level);
		// Logging-Dateien nutzen?
		boolean logToFile = false;
		if(Logger.loggable()){
			logToFile = Boolean.valueOf(prop.getProperty("logToFile"));
			if(logToFile){
				int interval = Integer.parseInt(prop
						.getProperty("logClearInterval"));
				Logger.setLogFile(prop.getProperty("logFile"), interval);
			}
		}

		Logger.info(TAG + " - Config",
				"Logging-Status: " + prop.getProperty("loggingEnabled"));
		Logger.info(TAG + " - Config",
				"Logging-Level: " + prop.getProperty("logLevel"));
		Logger.info(TAG + " - Config", "Nutze Log-Datei: " + logToFile);
		if(logToFile){
			Logger.info(TAG + " - Config",
					"Log-Datei: " + prop.getProperty("logFile"));
			Logger.info(
					TAG + " - Config",
					"Log Datei leeren nach: "
							+ prop.getProperty("logClearInterval"));
		}
	}
}
